package com.easy.work.function;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshListView;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.bean.WorkBaseInfo;
import com.easy.work.function.main.WorkBaseDetailAct;
import com.easy.work.function.main.adapter.MainListAdapter;
import com.easy.work.function.me.MyCollectAct;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.JobFilterRequest;
import com.easy.work.http.response.WorkBaseListInfoList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/1/7.
 * 加载筛选之后的职位列表
 */
public class JobFilterListAct extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
    public static void start(Context ctx, String title, JobFilterRequest request) {
        Intent intent = new Intent(ctx, JobFilterListAct.class);
        intent.putExtra("JobFilterRequest", request);
        intent.putExtra("title", title);
        ctx.startActivity(intent);
    }

    PullToRefreshListView refreshListView;
    ListView listView;
    //    /**
//     * 加载位置坐标
//     */
//    private int currentPage = 0;
    private JobFilterRequest request;
    /**
     * 招聘列表
     */
    List<WorkBaseInfo> workBaseInfoList = new ArrayList<>();
    MainListAdapter adapter;
    /**
     * 暂无记录
     */
    private ImageView imgNoData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_collect);
        refreshListView = (PullToRefreshListView) findViewById(R.id.listView);
        imgNoData = (ImageView) findViewById(R.id.img_no_data);

        if (getIntent() != null) {
            if (getIntent().hasExtra("JobFilterRequest")) {
                request = (JobFilterRequest) getIntent().getSerializableExtra("JobFilterRequest");
            }
            if (getIntent().hasExtra("title")) {
                initTitle(getIntent().getStringExtra("title"));
            } else {
                initTitle("职位");
            }
        }
        initListView();

    }

    /**
     * ListView初始化
     */
    private void initListView() {
        refreshListView.setPullLoadEnabled(true);
        refreshListView.setPullRefreshEnabled(true);
        refreshListView.setOnRefreshListener(this);


        listView = refreshListView.getRefreshableView();
        listView.setHeaderDividersEnabled(false);
        listView.setDividerHeight(0);


        adapter = new MainListAdapter(oThis, workBaseInfoList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WorkBaseInfo baseInfo = (WorkBaseInfo) parent.getAdapter().getItem(position);
                WorkBaseDetailAct.start(oThis, baseInfo.workId);
            }
        });

        AppSession.setPullFormartRefreshTime(refreshListView);
        refreshListView.doPullRefreshing(true, 0);
    }

    private void getWorkList() {
        if (request == null) {
            return;
        }
        request.start = adapter.getCount();
        request.limit = 8;
        request.city = AppSession.getInstance().getCityCode();
        request.latitude = AppSession.getInstance().getLatitude();
        request.longitude = AppSession.getInstance().getLontitude();
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                WorkBaseListInfoList recommendList = (WorkBaseListInfoList) response;
                if (recommendList != null && recommendList.list != null && recommendList.list.size() > 0) {

                    List<WorkBaseInfo> addList = recommendList.list;
                    workBaseInfoList.addAll(addList);
                }
                adapter.notifyDataSetChanged();
                if (adapter.getCount() < 1) {
                    imgNoData.setVisibility(View.VISIBLE);
                } else {
                    imgNoData.setVisibility(View.GONE);
                }
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        workBaseInfoList.clear();
        getWorkList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        getWorkList();
    }
}
